package com.ald.crossex.action.user;

import com.ald.crossex.action.BaseAction;
import com.ald.crossex.dto.EditGoodsTypeRequest;
import com.ald.crossex.dto.EditUserRequest;
import com.ald.crossex.dto.JqGridRequest;
import com.ald.crossex.dto.JqGridResponse;
import com.ald.crossex.entity.TCeCoreUser;
import com.ald.crossex.entity.TCeGoodsType;
import com.ald.crossex.service.GoodsService;
import com.ald.crossex.util.Page;
import com.ald.crossex.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by lishijiong on 15/3/31.
 */
@RequestMapping(value = "/admin/goods/")
@Controller
public class GoodsAction extends BaseAction {

    @Autowired
    private GoodsService goodsService;

    /**
     * 货物类型管理
     * @return
     */
    @RequestMapping(value = "/goodsTypeManage")
    public String sysUserManage() {
        return "admin/goods/goodsTypeManage";
    }

    /**
     * 寄送货物管理
     * @return
     */
    @RequestMapping(value = "/sendGoodsManage")
    public String appUserManage() {
        return "admin/goods/sendGoodsManage";
    }

    /**
     * 获取货物类型
     * @param request
     * @return
     */
    @RequestMapping(value = "listGoodsType")
    @ResponseBody
    public JqGridResponse listGoodsType(@RequestBody JqGridRequest request) {
        Page page = goodsService.listGoodsType(request);
        return PageUtils.pageToJqGridResponse(page);
    }

    /**
     * 获取要寄送的货物
     * @param request
     * @return
     */
    @RequestMapping(value = "listGoods")
    @ResponseBody
    public JqGridResponse listGoods(@RequestBody JqGridRequest request) {
        Page page = goodsService.listGoods(request);
        return PageUtils.pageToJqGridResponse(page);
    }

    /**
     * 新增、修改、删除货物类型
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/editGoodsType", method = RequestMethod.POST)
    public JqGridResponse editGoodsType(EditGoodsTypeRequest request) throws Exception {
        String oper = request.getOper();
        TCeGoodsType type = new TCeGoodsType();
        type.setName(request.getName());
        type.setValid(request.getValid());
        type.setSort(request.getSort());
        type.setRemark(request.getRemark());
        try{
            if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_ADD)) {
                goodsService.addGoodsType(type);
            } else if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_EDIT)) {
                type.setTid(request.getTid());
                goodsService.updateGoodsType(type);
            } else if (oper.equals(JqGridRequest.JQGRIDREQUEST_OPER_DEL)) {
                if(goodsService.delGoodsType(request.getId())<=0){
                    return new JqGridResponse(JqGridResponse.JQGRID_STATUS_ERROR);
                }
            }
        }catch (Exception e){
            return new JqGridResponse(JqGridResponse.JQGRID_STATUS_ERROR);
        }

        return new JqGridResponse(JqGridResponse.JQGRID_STATUS_SUCCESS);
    }

}
